package com.shipwe.loans.utils.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.shipwe.loans.R
import com.shipwe.loans.databinding.ScoreDialogBinding
import com.shipwe.loans.pro.ShipweDialog
import com.shipwe.loans.utils.event.AppEventPointHelper
import com.shipwe.loans.utils.event.AppEventPointType
import com.shipwe.loans.utils.helper.ScoreDialogHelper

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/13 16:26
 * @Annotation：评分 Normal
 */
class ScoreDialog: ShipweDialog {
    private lateinit var binding: ScoreDialogBinding
    private var mCallback: ScoreDialogHelper.IStarDialogCallback? = null

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: ScoreDialogHelper.IStarDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: ScoreDialogHelper.IStarDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = ScoreDialogBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
//        binding.scoreNotNow.paint.flags = Paint.UNDERLINE_TEXT_FLAG//下划线
//        binding.scoreNotNow.paint.isAntiAlias = true//抗锯齿
    }

    override fun initListener() {
        setOnClickListener(binding.scoreNotNow)
        setOnClickListener(binding.scoreRate)

        binding.ratingBar.setIsIndicator(false)
        binding.ratingBar.setOnRatingChangeListener { ratingBar, rating, fromUser ->
            AppEventPointHelper.appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_USER_RATE_US_STAR,rating.toInt().toString())
        }
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        when(v!!.id){
            R.id.scoreNotNow -> {//关闭、取消
                AppEventPointHelper.appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_USER_RATE_US_NOT_NOW,"")
                dismiss()
            }
            else -> {//确定
                val count = binding.ratingBar.rating.toInt()
                if (count <= 0){
                    return
                }
                if(mCallback != null){
                    mCallback!!.completeScoringCount(count)
                }
                dismiss()
            }
        }
    }

    fun setScoreTextTips(tips: String){
        binding.scoreTips.text = tips
    }

    override fun onStart() {
        super.onStart()
        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.DIALOG.SHIPWE_DIALOG_USER_RATE_SHOW,"Normal Dialog")
    }

    override fun dismiss() {
        super.dismiss()
        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.DIALOG.SHIPWE_DIALOG_USER_RATE_DISMISS,"Normal Dialog")
    }
}